﻿using System;
using System.Text;
using LogException.Dominio.Repositorios;
using System.Data.SqlClient;
using System.Data;

namespace LogException.InfraEstrutura
{
   public class RepositorioSql : ISql
    {
       long _logId;
       string _verificaTabelaDeLog = "SELECT ISNULL(name,0) FROM SYSOBJECTS WHERE TYPE='U' AND NAME = @nome;";
       object _retornoScalar;
       bool _existeTabelaLog;

        public long AdicionaLog(string innerException, string message, string source, string stacktrace, string stringDeConexao,string nomeTabela)
        {
            
            using (SqlConnection cn = new SqlConnection(stringDeConexao))
            {
                
                StringBuilder _insereLogSQL = new StringBuilder();
                _insereLogSQL.Append("INSERT INTO ");
                _insereLogSQL.Append(nomeTabela);
                _insereLogSQL.Append(" (innerException, message, source, stackTrace) values(@innerException, @message, @source, @stackTrace); SET @logId = scope_identity();");

                SqlCommand cmd = new SqlCommand(_insereLogSQL.ToString(), cn);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@innerException", SqlDbType.NVarChar).Value = innerException;
                cmd.Parameters.Add("@message", SqlDbType.NVarChar).Value = message;
                cmd.Parameters.Add("@source", SqlDbType.NVarChar).Value = source;
                cmd.Parameters.Add("@stackTrace", SqlDbType.NVarChar).Value =stacktrace;
                cmd.Parameters.Add("@logId", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                cn.Open();
                cmd.ExecuteNonQuery();
                _logId = (long)cmd.Parameters["@logId"].Value;
            }
            return _logId; 
        }

        public long AdicionaLog(string message, string source, string stringDeConexao, string nomeTabela)
        {
            using (SqlConnection cn = new SqlConnection(stringDeConexao))
            {
                
                StringBuilder _insereLogSQL = new StringBuilder();
                _insereLogSQL.Append("INSERT INTO ");
                _insereLogSQL.Append(nomeTabela);
                _insereLogSQL.Append(" (innerException, message, source, stackTrace) values(@innerException, @message, @source, @stackTrace); SET @logId = scope_identity();");

                SqlCommand cmd = new SqlCommand(_insereLogSQL.ToString(), cn);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@innerException", SqlDbType.NVarChar).Value = "-";
                cmd.Parameters.Add("@message", SqlDbType.NVarChar).Value = message;
                cmd.Parameters.Add("@source", SqlDbType.NVarChar).Value = source;
                cmd.Parameters.Add("@stackTrace", SqlDbType.NVarChar).Value = "-";
                cmd.Parameters.Add("@logId", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                cn.Open();
                cmd.ExecuteNonQuery();
                _logId = (long)cmd.Parameters["@logId"].Value;
            }
            return _logId; 
        }


        public bool ExisteTabelaDeLog(string nomeTabela, string stringDeConexao )
        {
            
            using (SqlConnection cn = new SqlConnection(stringDeConexao)) 
            {
                SqlCommand cmd = new SqlCommand(_verificaTabelaDeLog, cn);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@nome", SqlDbType.NVarChar).Value = nomeTabela;
                cn.Open();
                _retornoScalar = cmd.ExecuteScalar();
                _existeTabelaLog = ((_retornoScalar == null) || _retornoScalar == DBNull.Value) ? false : true;     
            }
                return _existeTabelaLog;
        }
        
    }
}
